<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="卡券编号" prop="id">
        <el-input v-model="queryParams.id" placeholder="卡券编号"
                  clearable size="mini" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="卡券名称" prop="id">
        <el-input v-model="queryParams.name" placeholder="卡券名称"
                  clearable size="mini" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="卡券状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable size="mini">
          <el-option v-for="dict in statusOptions" :key="dict.value" :label="dict.label" :value="+dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="卡券类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择" clearable size="mini">
          <el-option v-for="dict in typeOptions" :key="dict.value" :label="dict.label" :value="+dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="创建日期">
        <el-date-picker v-model="dateRange" size="mini" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPerm="['beauty:coupon:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPerm="['beauty:coupon:remove']">删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange" size="mini"
              :height="tableHeight">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" prop="id" width="200" align="center"/>
      <el-table-column label="卡券名称" align="center" prop="name"/>
      <el-table-column label="卡券类型" align="center" prop="type">
        <template slot-scope="{row}">
          <el-tag type="success" v-if="row.type==0">全场通用</el-tag>
          <el-tag type="info" v-else-if="row.type==1">指定分类</el-tag>
          <el-tag type="danger" v-else>指定商品</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="面值" align="center" prop="amount"/>
      <el-table-column label="使用门槛" align="center" prop="minPoint" width="100"/>
      <el-table-column label="有效期" align="center" width="200">
        <template slot-scope="{row}">
          <span>{{ row.startTime }}至{{ row.endTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数量">
        <template slot-scope="{row}">
          <div>发行数：{{ row.publishCount }}</div>
          <div>已领取：{{ row.receiveCount }}</div>
          <div>已使用：{{ row.useCount }}</div>
          <div>剩余数：{{ row.remainCount }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="{row}">
          <el-tag v-if="row.status === 1" type="success">未过期</el-tag>
          <el-tag v-else type="info">已过期</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建日期" align="center" prop="gmtCreate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtCreate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <router-link :to="`/coupon/detail/${scope.row.id}`">
            <el-button size="mini" type="text" icon="el-icon-document" v-if="scope.row.type!==0">详情
            </el-button>
          </router-link>
          <el-button size="mini" type="text" icon="el-icon-document" @click="handleDown(scope.row)"
                     v-hasPerm="['beauty:coupon:down']">
            下架
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPerm="['beauty:coupon:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页插件-->
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.current" :limit.sync="queryParams.size"
                @pagination="getList"/>

  </div>
</template>

<script>
import { delCoupon, listCoupon, downCoupon } from '@a/beauty/coupon'

export default {
  name: 'Coupon',
  data() {
    return {
      // 日期范围
      dateRange: [],
      tableHeight: window.innerHeight - 340,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 轮播图管理表格数据
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 卡券状态
      statusOptions: [],
      // 卡券类型
      typeOptions: [],
      // 查询参数
      queryParams: {
        // 当前页
        current: 1,
        // 每页显示条数
        size: 10,
        id: undefined,
        type: undefined,
        name: undefined,
        status: undefined
      }
    }
  },
  watch: {
    $route() {
      // 跳转到该页面后需要进行的操作
      this.getList()
    }
  },
  created() {
    this.getList()
    this.getDictItems('beauty_coupon_status').then(response => {
      this.statusOptions = response.data
    })
    this.getDictItems('beauty_coupon_type').then(response => {
      this.typeOptions = response.data
      console.log(this.typeOptions)
    })
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      listCoupon(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    // 下架
    handleDown(row) {
      this.$confirm('确定要下架吗', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return downCoupon(row.id)
      }).then(() => {
        this.getList()
        this.msgNoticeSuccess('下架成功')
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // 跳转到新增页面
      this.$router.push({ name: 'couponAdd' })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delCoupon(ids)
      }).then(() => {
        this.getList()
        this.msgNoticeSuccess('删除成功')
      })
    }
  }
}
</script>
<style lang="scss" scoped>

.dialog-coupon ::v-deep .el-dialog__body {
  height: 600px;

  .el-dialog {
    height: 87%;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    padding-left: 15px;
  }
}
</style>
